<template>
  <div>
    <el-dialog
      title="添加仓库"
      :visible.sync="Visible"
      width="50%">
      <el-form :model="addForm" :rules="rules" ref="addForm" label-width="100px" class="demo-ruleForm">
        <el-form-item label="仓库名" prop="warName">
          <el-input v-model="addForm.warName" placeholder="请输入仓库名" @blur="change_warName"></el-input>
        </el-form-item>
        <el-form-item label="仓库最大库存" prop="warMaxStock">
          <el-input v-model="addForm.warMaxStock" placeholder="请输入仓库最大库存"></el-input>
        </el-form-item>
        <goods_category ref="goods_category"></goods_category>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="yz('addForm')">提 交</el-button>
        <el-button @click="Visible = false">退 出</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
    import goods_category from "../goods_category";
    export default {
        name: "addWarehouseInfo",
        data(){
          return{
            Visible:false,
            addForm: {
              warName: '',
              warMaxStock: '',
              warehouseClassificationList:[]
            },
            rules: {
              warName: [
                { required: true, message: '请输入仓库名称', trigger: 'blur' }
              ],
              warMaxStock: [
                { required: true, message: '请输入仓库最大库存', trigger: 'blur' }
              ]
            }
          }
        },
      methods:{
        openDialog(){
          this.Visible=true;
        },
        yz(addForm) {
          this.$refs[addForm].validate((valid) => {
            if (valid) {
              this.submitForm()
            } else {
              return false;
            }
          });
        },
        submitForm() {
          //获取选择的商品分类，循环存入到对象集合中
          let ShopTypeInfos=this.$refs["goods_category"].ShopTypeInfos;
          ShopTypeInfos.forEach(item=>{
            this.addForm.warehouseClassificationList.push(
              {
                shopClassId:item
              }
            );
          })
          //调用异步，进行申请仓库添加
          this.$axios.put("warehouseInfo/warehouseInfo.action",this.addForm)
            .then(res=>{
              if(res.data.x){
                this.$message.success(res.data.msg);
              }else {
                this.$message.error(res.data.msg);
              }
              //关闭对话框
              this.Visible=false;
            })
            .catch(err=>{
              this.$message.warning(err);
              alert(err);
            })
        },
        //验证仓库名是否重复
        change_warName(){
          let warname=this.addForm.warName
          let WarehouseInfo=new URLSearchParams();
          if (warname != undefined && warname != "") {
            WarehouseInfo.append("warName", warname)
          }
          this.$axios.get("warehouseInfo/querybywarname.action",{params:WarehouseInfo}).then(res=>{
            if(res.data.code==1){
              this.addForm.warName=""
              this.warning()
            }
          }).catch(error=>{

          })
        },
        //失败后调用提示框方法
        warning() {
          this.$message({
            message: '该仓库名已被使用！',
            type: 'warning'
          });

        },
        resetForm(formName) {
          this.$refs[formName].resetFields();
        },
        getData(){
          this.$refs.goods_category.getAllshopTypeInfos();
        }
      },
      components:{
        goods_category
      },
    }
</script>

<style scoped>
  .text {
    font-size: 14px;
  }

  .item {
    margin-bottom: 18px;
  }

  .clearfix:before,
  .clearfix:after {
    display: table;
    content: "";
  }
  .clearfix:after {
    clear: both
  }

  .box-card {
    width: 480px;
  }
</style>
